<?php include "include/function_library.php"; ?>
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>

<?php include "html/header.html"; ?>

</head>
<body class="tcuk home">

	<div id="wp">
	
		<?php  include "html/menu.html"; ?>
		
		<!-- CONTENT STARTS-->
		<div id="ct"> 	
			
			<?php 
			/* SIGN UP FORM PHP PART */
						
			if(post("tour_submit")) {
				
				$tour_location 			= post("tour_location");
				$tour_name 				= post("tour_name");
				$tour_description 		= post("tour_description");
				$tour_start_date 		= post("tour_start_date");
				$tour_end_date 			= post("tour_end_date");
				$tour_type 				= post("tour_type");
				
				$organizer_company_id = "1";
				
				$query = "INSERT INTO tour (location, name, description, start_date, end_date, type, organizer_company_id) 
									VALUES ('$tour_location', '$tour_name', '$tour_description', '$tour_start_date', '$tour_end_date', '$tour_type', '$organizer_company_id')";
				
				$link = connect();
				$result = mysql_query($query, $link) or die("Sql Error! " . mysql_error());	
				$last_added_tour_id = mysql_insert_id();
								
				foreach($_POST["hotels_of_tour"] as $hotel) {
					$query = "INSERT INTO hotel_of_tour (tour_id, hotel_id)
									VALUES ('$last_added_tour_id', '$hotel')";
					$result = $result && (mysql_query($query, $link) or die("Sql Error! " . mysql_error()));
				}
				mysql_close($link);
				
				if($result) {
					echo "<div class='alert success'>Tour Saved Successfully! You can <a href='tour_list.php'>take a look </a>at tour list.</div>";
				} else {
					echo "<div class='alert error'>Error! Could Not Save! There is(are) error(s) in the form!</div>";
				}
			}
			
			$queryHotels = "SELECT id, name FROM hotel";
			$resultOfHotels = sql_query($queryHotels);
			?>
			<br/><h2>Add Tour</h2>
			<!-- SIGN UP FORM HTML PART -->
			<form action="<?php echo curPageURL(); ?>" method="post">
			<ul id="signup_form" style="width: 50%; margin-left: auto; margin-right: auto; margin-top: 30px; list-style: none;">
				<li style="border: none;">
					<label>Name </label>
					<input type="text" id="tour_name" name="tour_name" value="" placeholder="Tour name.." style="width: 250px;"/>
				</li>
				<li>
					<label>Location </label>
					<input type="text" id="tour_location" name="tour_location" value="" placeholder="Location.." style="width: 250px;"/>
				</li>
				<li>
					<label>Description </label>
					<textarea rows="10" id="tour_description" name="tour_description" placeholder="Description.." style="width: 250px;"></textarea>
				</li>
				<li>
					<label>Start Date</label>
					<input type="date" id="tour_start_date" name="tour_start_date" value="" placeholder="Start date.." style="width: 120px;"/>
				</li>
				<li>
					<label>End Date </label>
					<input type="date" id="tour_end_date" name="tour_end_date" value="" placeholder="End date.." style="width: 120px;"/>
				</li>
				<li>
					<label>Type </label>
					<select id="tour_type" name="tour_type">
						<option value="domestic">Domestic</option>
						<option value="abroad">Abroad</option>
					</select>
				</li>
				<li>
					<label>Hotels(press ctrl for multiple selection) </label>
					
					<select id="hotels_of_tour[]" name="hotels_of_tour[]" multiple>
					<?php 
					while($tuple = mysql_fetch_assoc($resultOfHotels)) {
						?>
						<option value="<?php echo $tuple["id"] ?>"><?php echo $tuple["name"] ?></option>
						<?php
					}?>
						
					</select>
				</li>
				<li>
					<input type="submit" id="tour_submit" name="tour_submit" value="Save" style="width: 90px; height:30px; margin-left: 80px;"/>
				</li>
			</ul>
			</form>
		
		</div> 	
		<!-- CONTENT ENDS-->		
		
		<?php include "html/footer.html"; ?>
		
	</div>
	
</body>
</html>